package com.augurit.wf.jbpmex.cmd;

import org.jbpm.api.ProcessInstance;
import org.jbpm.api.TaskService;
import org.jbpm.api.cmd.Environment;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.jbpm.pvm.internal.model.TransitionImpl;
import org.jbpm.pvm.internal.session.DbSession;
import org.jbpm.pvm.internal.session.RepositorySession;

public class ActivityJumpCmd extends AbstractCommand<Void>{


	private static final long serialVersionUID = -5996415196851997153L;
	
    
	protected String destinationActivityName;
	protected String executionId;
	
	
	private ActivityImpl source;
	private TransitionImpl tempTransition;
	
	public ActivityJumpCmd(String executionId,String destinationActivityName){
		this.executionId = executionId;
		this.destinationActivityName = destinationActivityName;
	}
	
	/**
	 * 串行跳转
	 * */
	public Void execute(Environment environment) throws Exception {
		//创建跳转
		creatTransition(environment);
		 
		//完成当前节点任务
		compleNodeTask(environment);
		 
		//去掉创建的转向
		removeNodeTransition();
		 
		return null;
	}
	
	//获取目标activity
	public ActivityImpl getActivityByName(RepositorySession repositorySession, ExecutionImpl execution, String activityName){
		ProcessDefinitionImpl processDefinition = repositorySession.findProcessDefinitionById(execution.getProcessDefinitionId());
		ActivityImpl destination = processDefinition.getActivity(activityName);
		return destination;
	}
	
	//创建transition
	public void creatTransition(Environment environment){
		RepositorySession repositorySession = environment.get(RepositorySession.class);
		DbSession dbSession = environment.get(DbSession.class);
		
		ExecutionImpl execution = (ExecutionImpl)dbSession.findExecutionById(executionId);
		ActivityImpl source = execution.getActivity();
		
		TransitionImpl transition = source.createOutgoingTransition();
		
		transition.setSource(source);
		transition.setName(this.destinationActivityName);
		
		ActivityImpl destination = this.getActivityByName(repositorySession, execution, destinationActivityName);
		transition.setDestination(destination);
		
		//设置私有属性,为去掉创建的transition做准备
		this.tempTransition = transition;
		this.source = source;
	}
	
	//完成当前active节点的任务
	public void compleNodeTask(Environment environment){
		TaskService taskService = environment.get(TaskService.class);
		DbSession dbSession = environment.get(DbSession.class);
		ExecutionImpl execution = (ExecutionImpl)dbSession.findExecutionById(executionId);
		ProcessInstance processInstance = execution.getProcessInstance();
		ActivityImpl activity =  execution.getActivity();
		String processInstanceId = processInstance.getId();
		Task task = dbSession.createTaskQuery().processInstanceId(processInstanceId)
						.activityName(activity.getName()).uniqueResult();
		taskService.completeTask(task.getId(),destinationActivityName);
	}
	
	
	//去掉建立的transition
	public void removeNodeTransition(){
		this.source.removeOutgoingTransition(tempTransition);
	}

}
